package cn.iocoder.yudao.module.vote.dal.mysql.voteuserresult;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.vote.controller.admin.fielddynamicsconfig.vo.FieldInfoVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserresult.vo.VoteUserResultExportReqVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserresult.vo.VoteUserResultGroupVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserresult.vo.VoteUserResultPageReqVO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserresult.VoteUserResultDO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserresultdetail.VoteUserResultDetailDO;
import cn.iocoder.yudao.module.vote.utils.FieldUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * 投票结果 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface VoteUserResultMapper extends BaseMapperX<VoteUserResultDO>{

    default PageResult<VoteUserResultDO> selectPage(VoteUserResultPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<VoteUserResultDO>()
                .eqIfPresent(VoteUserResultDO::getUserId, reqVO.getUserId())
                .eqIfPresent(VoteUserResultDO::getVoteId, reqVO.getVoteId())
                .eqIfPresent(VoteUserResultDO::getVoteType, reqVO.getVoteType())
                .likeIfPresent(VoteUserResultDO::getVoteName, reqVO.getVoteName())
                .eqIfPresent(VoteUserResultDO::getMobile, reqVO.getMobile())
                .likeIfPresent(VoteUserResultDO::getUserName, reqVO.getUserName())
                .eqIfPresent(VoteUserResultDO::getQuantifiedScore, reqVO.getQuantifiedScore())
                .eqIfPresent(VoteUserResultDO::getTotal, reqVO.getTotal())
                .eqIfPresent(VoteUserResultDO::getWeight, reqVO.getWeight())
                .eqIfPresent(VoteUserResultDO::getRemark, reqVO.getRemark())
                .eqIfPresent(VoteUserResultDO::getVotePersonType, reqVO.getVotePersonType())
                .eqIfPresent(VoteUserResultDO::getVoteReviewerType, reqVO.getVoteReviewerType())
                .eqIfPresent(VoteUserResultDO::getPosition, reqVO.getPosition())
                .eqIfPresent(VoteUserResultDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(VoteUserResultDO::getGroupName, reqVO.getGroupName())
                .betweenIfPresent(VoteUserResultDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
//                .orderByDesc(VoteUserResultDO::getApplyType)
                .orderByDesc(reqVO.getVoteId()!=null,VoteUserResultDO::getQuantifiedScore)
                .orderByDesc(reqVO.getVoteId()==null,VoteUserResultDO::getCreateTime)
        );
    }

    default List<VoteUserResultDO> selectList(VoteUserResultExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<VoteUserResultDO>()
                .eqIfPresent(VoteUserResultDO::getUserId, reqVO.getUserId())
                .eqIfPresent(VoteUserResultDO::getVoteId, reqVO.getVoteId())
                .eqIfPresent(VoteUserResultDO::getVoteType, reqVO.getVoteType())
                .likeIfPresent(VoteUserResultDO::getVoteName, reqVO.getVoteName())
                .eqIfPresent(VoteUserResultDO::getMobile, reqVO.getMobile())
                .likeIfPresent(VoteUserResultDO::getUserName, reqVO.getUserName())
                .eqIfPresent(VoteUserResultDO::getQuantifiedScore, reqVO.getQuantifiedScore())
                .eqIfPresent(VoteUserResultDO::getTotal, reqVO.getTotal())
                .eqIfPresent(VoteUserResultDO::getWeight, reqVO.getWeight())
                .eqIfPresent(VoteUserResultDO::getRemark, reqVO.getRemark())
                .eqIfPresent(VoteUserResultDO::getVotePersonType, reqVO.getVotePersonType())
                .eqIfPresent(VoteUserResultDO::getPosition, reqVO.getPosition())
                .eqIfPresent(VoteUserResultDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(VoteUserResultDO::getGroupName, reqVO.getGroupName())
                .betweenIfPresent(VoteUserResultDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByAsc(VoteUserResultDO::getGroupName,VoteUserResultDO::getWeight));
//                .orderByAsc(reqVO.getIsQuote()!=null && reqVO.getIsQuote(),VoteUserResultDO::getApplyType));
    }


    default VoteUserResultDO selectByMobile(Integer voteId,String mobile) {
        return selectOne(new LambdaQueryWrapper<VoteUserResultDO>()
                .eq(VoteUserResultDO::getMobile, mobile)
                .eq(VoteUserResultDO::getVoteId,voteId)
        );
    }

    default Long countByVoteId(Integer voteId) {
        return selectCount(new LambdaQueryWrapper<VoteUserResultDO>().eq(VoteUserResultDO::getVoteId, voteId));
    }

    default Long countByCreater(Long userId,Integer voteId) {
        return selectCount(new LambdaQueryWrapper<VoteUserResultDO>()
                .eq(VoteUserResultDO::getCreator, userId)
                .eq(VoteUserResultDO::getVoteId,voteId)
        );
    }

    default List<VoteUserResultDO> getByVoteId(Integer voteId) {
        return selectList(new LambdaQueryWrapper<VoteUserResultDO>()
                .eq(VoteUserResultDO::getVoteId,voteId)
        );
    }

    default List<VoteUserResultDO> getByVoteIdAndGroupName(Integer voteId, Integer groupId, Predicate<TableFieldInfo> predicate, List<FieldInfoVO> orderList) {

        QueryWrapper<VoteUserResultDO> queryWrapper = new QueryWrapper<>();

        for(FieldInfoVO order:orderList){
            if(order!=null) {
                queryWrapper.orderBy(true,order.getIsAsc(),order.getFieldName());
            }
        }
        queryWrapper
                .lambda()
                .select(VoteUserResultDO.class,predicate)
                .eq(VoteUserResultDO::getVoteId,voteId)
                .eq(VoteUserResultDO::getGroupId,groupId);
//                .eq(VoteUserResultDO::getGroupName,groupName);


        return selectList(queryWrapper);
    }

    default Long countByDeptId(Long deptId){
        return selectCount(new LambdaQueryWrapperX<VoteUserResultDO>()
                .eq(VoteUserResultDO::getGroupId, deptId));
    }

    /**
     * 统计组数
     * @param voteId
     * @param groupName
     * @return
     */
    List<VoteUserResultGroupVO> groupByGroupName(@Param("voteId") Integer voteId,@Param("groupName") String groupName);
    /**
     * 统计组数
     * @param voteId
     * @param deptId
     * @return
     */
    List<VoteUserResultDO> getByUnitName(@Param("voteId") Integer voteId,@Param("deptId") Long deptId,@Param("voteReviewerType") Integer voteReviewerType);
    /**
     * 统计组数
     * @param voteId
     * @param ip
     * @return
     */
    List<VoteUserResultDO> getByVoteIdAndIp(@Param("voteId") Integer voteId,@Param("ip") String ip);




    /**
     * 统计每组内的排名
     * @param voteId
     * @param applyType
     * @return
     */
    List<VoteUserResultGroupVO> countByGroupNameSort(@Param("voteId") Integer voteId,@Param("applyType") String applyType);




}
